package com.example.hzq.zqb.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hzq.zqb.R;
import com.example.hzq.zqb.bean.NewsBean;
import com.squareup.picasso.Picasso;

import java.util.List;

import static com.example.hzq.zqb.fragment.Fragment1.imageurl;

/**
 * Created by hzq on 2017/7/14.
 */

public class NewsAdapter extends RecyclerView.Adapter {
    private Context context;
    private LayoutInflater layoutInflater;//动态加载布局
    private List<NewsBean>list;
    public NewsAdapter(Context context) {
        this.context=context;
        layoutInflater=LayoutInflater.from(context);
    }
    public void setList(List<NewsBean> list){
        this.list=list;
        notifyDataSetChanged();
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        private TextView send,comment,time;
        private ImageView imageView;
        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.news_pic);
            send=(TextView)itemView.findViewById(R.id.news_send);
            comment=(TextView)itemView.findViewById(R.id.news_comment);
            time=(TextView)itemView.findViewById(R.id.news_time);
        }
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = layoutInflater.inflate(R.layout.newsitem, parent, false);
        return new ViewHolder(v);
    }
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final NewsBean newBean = list.get(position);
        if (newBean == null)
            return;
        final ViewHolder viewholder = (ViewHolder) holder;
        Picasso.with(context).load(imageurl+newBean.getImg()).placeholder(R.drawable.ic_default_adimage).into(viewholder.imageView);
        viewholder.send.setText(newBean.getSuid());
        viewholder.comment.setText(newBean.getContent());
        viewholder.time.setText(newBean.getTime());
        viewholder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context,newBean.getSuid(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        if(list==null)
            return 0;
        return list.size();
    }
}

